//
//  CourseDetailsIntroduceView.m
//  ZFPlayer_Example
//
//  Created by 洪欣 on 2019/1/4.
//  Copyright © 2019年 紫枫. All rights reserved.
//

#import "CourseDetailsIntroduceView.h"
#import "ZFTableData.h"
#import "UIImageView+ZFCache.h"
#import <UIImageView+WebCache.h>

static NSString *fkVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";

@interface CourseDetailsIntroduceView ()
@property (weak, nonatomic) IBOutlet UIView *videoView;
@property (weak, nonatomic) IBOutlet UIImageView *videoCoverView;
@property (weak, nonatomic) IBOutlet UIButton *playVideoBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLb;
@property (weak, nonatomic) IBOutlet UILabel *contentLb;
@end

@implementation CourseDetailsIntroduceView

-(void)awakeFromNib {
    [super awakeFromNib];
    [self.videoCoverView setImageWithURLString:@"" placeholder:[UIImage imageNamed:@"default_cover"]];
    /// playerManager
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    /// player的tag值必须在cell里设置
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.videoView];
    self.player.controlView = self.controlView;
    /// 设置退到后台继续播放
    self.player.pauseWhenAppResignActive = NO;
    
    [self.controlView.portraitControlView.kuaiButton addTarget:self action:@selector(clickKuaijinBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.controlView.portraitControlView.playBtn addTarget:self action:@selector(clickPlayBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.controlView.landScapeControlView.kuaijinBtn addTarget:self action:@selector(clickKuaijinBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.controlView.landScapeControlView.playOrPauseBtn addTarget:self action:@selector(clickPlay:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
#pragma mark - 这里是快进按钮，
- (void)clickKuaijinBtn:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.player.currentPlayerManager.rate = 2.0;
    }else{
        self.player.currentPlayerManager.rate = 1.0;
    }
    
    
}
#warning TODO - 怎么样拿到  恢复正常播放速度 事件？
- (void)clickPlayBtn:(UIButton *)sender{
    self.player.currentPlayerManager.rate = 1.0;
}
- (void)clickPlay:(UIButton *)sender{
    self.player.currentPlayerManager.rate = 1.0;
}
- (void)setModel:(CourseDetailsDataModel *)model {
    _model = model;
//    self.titleLb.text = model.packTitle;
//    self.contentLb.text = model.packageInfo;
//
//    CourseDetailsCourseOutputDTODataModel *cellModel = model.courseOutputDTOList.firstObject;
//    CourseDetailsCourseHourOutputDTODataModel *subModel = cellModel.courseHourOutputDTOList.firstObject;
//    NSString *urlString = [Public_URL stringByAppendingString:subModel.videoImage];
//    [self.videoCoverView sd_setImageWithURL:[NSURL URLWithString:urlString]];
    
//    self.titleLb.text = model.packTitle;
    self.contentLb.text = model.packagePresent;
#warning TODO - 这里返回的字段是空字段 让后天添加数据后替换
    NSString *urlString = [NSString stringWithFormat:@"%@%@",Public_URL,model.imgUrl];
    
    [self.videoCoverView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"default_cover"]];
    CourseDetailsCourseOutputDTODataModel *cellModel = model.courseOutputDTOList.firstObject;
    CourseDetailsCourseHourOutputDTODataModel *subModel = cellModel.courseHourOutputDTOList.firstObject;
    self.currentSubModel = subModel;
}

- (void)setCurrentSubModel:(CourseDetailsCourseHourOutputDTODataModel *)currentSubModel {
    _currentSubModel = currentSubModel;
    NSString *url = [NSString stringWithFormat:@"%@%@",Public_URL,currentSubModel.videoImage];
    
//    [self.videoCoverView sd_setImageWithURL:[NSURL URLWithString:url]];
    
    [self.player stop];
    [self.controlView resetControlView];
    //课时类型 0 视频, 1 音频 , 2 图文混排
    if (currentSubModel.hourType.integerValue == 2) {
        self.playVideoBtn.hidden = YES;
    }else {
        self.playVideoBtn.hidden = NO;
    }
    self.playVideoBtn.hidden = YES;
}

- (IBAction)didPlayVideoClick:(UIButton *)button {
//    if ([self.model2.data.isPay integerValue]==0) {
//        [MBProgressHUD showSuccess:@"请先购买此课程" toView:self];
//        return ;
//    }
//    if (self.currentSubModel.hourType.integerValue == 0) {
//        // 视频
//        NSLog(@"播放视频");
//    }else if (self.currentSubModel.hourType.integerValue == 1) {
//        // 音频
//        NSLog(@"播放音频");
//    }
    
//    NSString *URLString = [NSString stringWithFormat:@"%@%@",Public_URL,self.currentSubModel.hourUrl];
//    NSString *encodeUrlString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    NSLog(@"%@",URLString);
//    self.player.assetURL = [NSURL URLWithString:encodeUrlString];
//    self.controlView.placeholderImage = self.videoCoverView.image;
//    [self.controlView showTitle:nil/**self.currentSubModel.hourName*/ coverURLString:[NSString stringWithFormat:@"%@%@",Public_URL,self.currentSubModel.videoImage] fullScreenMode:ZFFullScreenModeLandscape];
//    NSString *baid = [NSString stringWithFormat:@"%@%@",Public_URL,self.currentSubModel.videoImage];
//    [self.player.currentPlayerManager play];
//    NSLog(@"%@",baid);
//    NSString *URLString = @"https://www.apple.com/105/media/cn/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/bruce/mac-bruce-tpl-cn-2018_1280x720h.mp4";
//    self.player.assetURL = [NSURL URLWithString:URLString];
//    [self.controlView showTitle:@"Apple" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
    }
    return _controlView;
}

@end
